/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package dawnland03.model.action.handler;

import dawnland03.model.action.Action;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * @author Petru Obreja (obrejap@yahoo.com)
 */
public class ActionHandler implements HandleStrategy{
    private static HandleStrategy outputDisplayHandleStrategy;
    private static HandleStrategy uiHandleStrategy;

    static {
        outputDisplayHandleStrategy = new OutputDisplayHandleStrategy();        
    }

    private void uiHandle(Action action){
        if(getUIHandleStrategy()!=null){
            getUIHandleStrategy().handle(action);
        }
    }

    private void outputDisplayHandle(Action action){
        outputDisplayHandleStrategy.handle(action);
    }

    private HandleStrategy getUIHandleStrategy(){
        if(uiHandleStrategy==null){
            try {
                Class classUIHandleStrategy = Class.forName("dawnland03.ui.model.action.handler.UIHandleStrategy");
                Method getInstanceMethod = classUIHandleStrategy.getMethod("getInstance");
                uiHandleStrategy = (HandleStrategy) getInstanceMethod.invoke(null);
            } catch (ClassNotFoundException ex) {
//                Logger.getLogger(ActionHandler.class.getName()).log(Level.SEVERE, null, ex);
            } catch (NoSuchMethodException ex) {
//                Logger.getLogger(ActionHandler.class.getName()).log(Level.SEVERE, null, ex);
            } catch (SecurityException ex) {
//                Logger.getLogger(ActionHandler.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
//                Logger.getLogger(ActionHandler.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalArgumentException ex) {
//                Logger.getLogger(ActionHandler.class.getName()).log(Level.SEVERE, null, ex);
            } catch (InvocationTargetException ex) {
//                Logger.getLogger(ActionHandler.class.getName()).log(Level.SEVERE, null, ex);
            }

        }
        return uiHandleStrategy;
    }

    public void handle(Action action) {
        outputDisplayHandle(action);
        uiHandle(action);
    }
}
